#include <atomic>
#include <iostream>
#include <thread>
#include <vector>

/**
 * @brief 使用 relaxed 仅做原子性保证，不同步其他数据。
 */
int main() {
  std::atomic<int> hits{0};
  constexpr int kThreads = 4;
  constexpr int kHitsPerThread = 25'000;

  auto recordHit = [&]() {
    for (int i = 0; i < kHitsPerThread; ++i) {
      hits.fetch_add(1, std::memory_order_relaxed);  ///< 只关心最终计数。
    }
  };

  std::vector<std::thread> workers;
  workers.reserve(kThreads);
  for (int i = 0; i < kThreads; ++i) {
    workers.emplace_back(recordHit);
  }
  for (auto& t : workers) {
    t.join();
  }

  std::cout << "relaxed hits: " << hits.load(std::memory_order_relaxed) << '\n';
  return 0;
}